package cn.gz.dzy.hm.sjjg.binarysearch;

/**
 * @program: java-interview
 * @description:
 * @author: DingZhenYun
 * @create: 2021-11-09 15:45
 **/

public class BinarySearch {
    public static void main(String[] args) {
        int[] array = {1, 5, 8, 11, 19, 22, 31, 35, 40, 45, 48, 49, 50};
        int target = 45;
        int idx = binarySearch(array, target);
        System.out.println(idx);
    }

    private static int binarySearch(int[] array, int target) {
        int left=0;
        int right=array.length-1;
        int mid=0;
        while(left<=right){
            mid=(left+right)/2;
            if(array[mid]==target){
                return mid;
            }else if(array[mid]>target){
                right=mid-1;
            }else {
                left=mid+1;
            }
        }
        return 0;
    }

}
